package com.shandong.controller;

import com.shandong.model.Result;
import com.shandong.support.RedisOperator;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.shandong.constant.RedisConstants.DEFAULT_TIMEOUT_SECONDS;
import static com.shandong.constant.RedisConstants.SMS_CAPTCHA_PREFIX_KEY;

/**
 * 登录接口，登录使用的接口
 *
 * @author wonangqi
 */
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final RedisOperator<String> redisOperator;

    @GetMapping("/getSmsCaptcha")
    public Result<String> getSmsCaptcha(String phone) {
        // 示例项目，固定1234
        String smsCaptcha = "666999";
        // 存入缓存中，5分钟后过期
        redisOperator.set((SMS_CAPTCHA_PREFIX_KEY + phone), smsCaptcha, DEFAULT_TIMEOUT_SECONDS);
        return Result.success("获取短信验证码成功.", smsCaptcha);
    }

//    @GetMapping("/getCaptcha")
//    public Result<CaptchaResult> getCaptcha() {
//        // 字符串挑选模板
//        String baseCharNumber = "abcdefghijklmnopqrstuvwxyz".toUpperCase() + "abcdefghijklmnopqrstuvwxyz0123456789";
//        // 随机字符串长度
//        int randomLength = 4;
//
//        StringBuilder captchaBuilder = new StringBuilder(randomLength);
//
//        for(int i = 0; i < randomLength; ++i) {
//            int number = ThreadLocalRandom.current().nextInt(baseCharNumber.length());
//            captchaBuilder.append(baseCharNumber.charAt(number));
//        }
//
//        String captcha = captchaBuilder.toString();
//        // 生成一个唯一id
//        long id = IdWorker.getId();
//        // 存入缓存中，5分钟后过期
//        redisOperator.set((IMAGE_CAPTCHA_PREFIX_KEY + id), captcha, DEFAULT_TIMEOUT_SECONDS);
//        return Result.success("获取验证码成功.", new CaptchaResult(String.valueOf(id), captcha, (null)));
//    }
//
//    @GetMapping("/getCaptcha1")
//    public Result<CaptchaResult> getCaptcha1() {
//        // 使用huTool-captcha生成图形验证码
//        // 定义图形验证码的长、宽、验证码字符数、干扰线宽度
//        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(130, 34, 4, 2);
//        // 生成一个唯一id
//        long id = IdWorker.getId();
//        // 存入缓存中，5分钟后过期
//        redisOperator.set((IMAGE_CAPTCHA_PREFIX_KEY + id), captcha.getCode(), DEFAULT_TIMEOUT_SECONDS);
//        return Result.success("获取验证码成功.", new CaptchaResult(String.valueOf(id), captcha.getCode(), captcha.getImageBase64Data()));
//    }

}
